package tw.com.softleader.fire.setting.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import tw.com.softleader.fire.common.dao.GenericDao;
import tw.com.softleader.fire.setting.entity.AcuEntity;
import tw.com.softleader.fire.setting.entity.DeductibleEntity;
/**
 * 
 * @author mdoing2001
 *
 */
@Repository
public class DeductibleDao extends GenericDao<DeductibleEntity> {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public List<DeductibleEntity> findByCode(DeductibleEntity entity){
		
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM set_deductible WHERE code like ? ");
		
		Query query =  session.createSQLQuery(
				sql.toString()).addEntity(DeductibleEntity.class);
		
		if(StringUtils.isEmpty(entity.getCode())){
			entity.setCode("%");
		}else {
			entity.setCode("%"+entity.getCode()+"%");
		}
		
		List<DeductibleEntity> models = query.setString(0, entity.getCode()).list();
		
		return models;
		
	}
}
